package org.duiduo.controller;

import io.jsonwebtoken.Claims;
import org.duiduo.entity.User;
import org.duiduo.service.UserService;
import org.duiduo.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * BaseController
 *
 * @author liangze
 */
@RestController
public class BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private JwtUtils jwtUtils;

    /**
     * 根据token获取用户信息
     *
     * @return user
     */
    public User getUser() {
        String token = request.getHeader("token");
        // String aud = JWT.decode(token).getAudience().get(0);
        // Long userId = Long.valueOf(aud);
        Claims claims = jwtUtils.getClaimsByToken(token);
        String username = claims.getSubject();
        return userService.getByUsername(username);
    }

}
